//
//  LeftSideVC.swift
//  GoTravel
//
//  Created by lanou3g on 16/3/4.
//  Copyright © 2016年 SHH. All rights reserved.
//

import UIKit

class LeftSideVC: UIViewController,SHHLoginVCDelegate,SHHPersonCenterTVCDelegate {

    
    
    @IBOutlet weak var headImageView: UIImageView!
    
    @IBOutlet weak var nameBtn: UIButton!
    
    
     override func viewDidLoad() {
        super.viewDidLoad()

        headImageView.layer.cornerRadius = 35
        headImageView.layer.masksToBounds = true
       
        let user = AVUser.currentUser()
        if user == nil {
            print("妹纸")
        }else{
            print(user["user_id"])
            let query = AVQuery(className: "UserInfo")
            query.whereKey("user_id", equalTo: user["user_id"])
            let userArray = query.findObjects()
            print("我尼玛")
            print("========\(userArray)")
            if userArray == nil || userArray.isEmpty{
                print("空")
                self.nameBtn.setTitle(user.username, forState: UIControlState.Normal)
                
            }else{
                let resultUser = userArray.first
                self.nameBtn.setTitle((resultUser!["name"] as! String), forState: UIControlState.Normal)
            }
//            let resultUser = userArray.first
//            print(resultUser!["name"] as! String)
//            self.nameBtn.setTitle((resultUser!["name"] as! String), forState: UIControlState.Normal)
        }
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        print("我尼玛")
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //尚未设置昵称
    @IBAction func nameSet(sender: AnyObject) {
        
        
        
    }
    

    //时间探索
    @IBAction func WordTravelAction(sender: AnyObject) {
        
        let wordVC = WordTravelVC()
        
        let nav = UINavigationController(rootViewController: wordVC)
        
        //替换当前视图
        self.sideMenuViewController.setContentViewController(nav, animated: true)
        
        //隐藏菜单视图
        self.sideMenuViewController.hideMenuViewController()
        
    }
    
    //灵感页面
    @IBAction func InspirationAction(sender: AnyObject) {
        
        let inspirationVC = InspirationVC()
        let nav = UINavigationController(rootViewController: inspirationVC)
        //替换当前视图
        self.sideMenuViewController.setContentViewController(nav, animated: true)
        
        //隐藏菜单视图
        self.sideMenuViewController.hideMenuViewController()
        
    }
    
    
    //游记
    @IBAction func TravelNotesAction(sender: AnyObject) {
        
//        if self.sideMenuViewController.contentViewController.isMemberOfClass(UINavigationController.classForCoder()) {
//            if ((self.sideMenuViewController.contentViewController.childViewControllers.first?.isMemberOfClass(RXRTravelNotesVC.classForCoder())) != nil) {
//                self.sideMenuViewController .hideMenuViewController()
//                return
//            }
//        }
        
        let travelNotesVC = RXRTravelNotesVC()
        
        let nav = UINavigationController(rootViewController: travelNotesVC)
        //替换当前视图
        self.sideMenuViewController.setContentViewController(nav, animated: true)
        
        //隐藏菜单视图
        self.sideMenuViewController.hideMenuViewController()
        
    }
    
    //写游记
    @IBAction func WriteTravelNotesAction(sender: AnyObject) {
        
        let writeTravelNotesVC = ZLQWroteTravelNotesVC()
        let nav = UINavigationController(rootViewController: writeTravelNotesVC)
        //替换当前视图
        self.sideMenuViewController.setContentViewController(nav, animated: true)
        
        //隐藏菜单视图
        self.sideMenuViewController.hideMenuViewController()
        
    }
    
    
    //个人中心
    @IBAction func PersonCenter(sender: AnyObject) {
        
        if AVUser.currentUser()?.username == nil {
            let login = SHHLoginVC()
//            self.changeState()
            login.delegate = self
            login.myBlock = {
                (str) -> Void in
                self.nameBtn.setTitle(str, forState: UIControlState.Normal)
            }
            let loginNC = UINavigationController(rootViewController: login)
            self.presentViewController(loginNC, animated: true, completion: nil)
        }else{
            let personCenterVC = SHHPersonCenterTVC()
//            self.changeLoginState()
            personCenterVC.delegate = self
            let nav = UINavigationController(rootViewController: personCenterVC)
            //替换当前视图
            self.sideMenuViewController.setContentViewController(nav, animated: true)
            
            //隐藏菜单视图
            self.sideMenuViewController.hideMenuViewController()
        }
        
        
    }
    
    //登录代理
    func changeState() {
        print("登录代理")
        
        let user = AVUser.currentUser()
        if user == nil {
            
        }else{
            let currentUser = SHHGetUser.sharedGetUser!.getUserInfo()
            if currentUser.photo_url == "" || currentUser.photo_url == nil {
                self.headImageView.image = UIImage(named: "iconfont-unie64d")
            }else{
                print(currentUser.photo_url!)
                self.headImageView.sd_setImageWithURL(NSURL(string: currentUser.photo_url!), placeholderImage: UIImage(named: "iconfont-unie64d"))
            }
            
            if currentUser.name == "" || currentUser.name == nil {
                self.nameBtn.setTitle("尚未设置昵称", forState: UIControlState.Normal)
            }else{
                self.nameBtn.setTitle(currentUser.name, forState: UIControlState.Normal)
            }
        }
        
        
    }
    
    //改变登录的状态
    func changeLoginState() {
       checkoutUserLogin()
        
    }
    
    func checkoutUserLogin() {
        let user = AVUser.currentUser()
        if user == nil {
            self.headImageView.image = UIImage(named: "HTLeftMenu_Head")
            self.nameBtn.setTitle("未登录", forState: UIControlState.Normal)
            let worldVC = WordTravelVC()
            let nav = UINavigationController(rootViewController: worldVC)
            self.sideMenuViewController.setContentViewController(nav, animated: true)
        }else{
            let currentUser:SHHGetUser? = SHHGetUser.sharedGetUser!.getUserInfo()
            
            if currentUser?.photo_url == "" || currentUser?.photo_url == nil {
                self.headImageView.image = UIImage(named: "iconfont-unie64d")
            }else{
                print(currentUser?.photo_url!)
                self.headImageView.sd_setImageWithURL(NSURL(string: (currentUser?.photo_url)!), placeholderImage: UIImage(named: "iconfont-unie64d"))
            }
            self.nameBtn.setTitle(currentUser?.name, forState: UIControlState.Normal)
            
        }
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
